package cn.drenal.capcap.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.drenal.capcap.common.Response;
import cn.drenal.capcap.entity.User;
import cn.drenal.capcap.entity.dto.UserDTO;
import cn.drenal.capcap.entity.vo.UserListVO;
import cn.drenal.capcap.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/user/manage")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 创建用户
     *
     * @param userDTO 用户信息
     * @return 创建结果
     */
    @PostMapping
    @SaCheckPermission("system:user:manage:add")
    public Response<User> createUser(@RequestBody UserDTO userDTO) {
        return Response.success(userService.createUser(userDTO));
    }

    /**
     * 更新用户信息
     *
     * @param id 用户ID
     * @param userDTO 用户信息
     * @return 更新结果
     */
    @PutMapping("/{id}")
    @SaCheckPermission("system:user:manage:edit")
    public Response<User> updateUser(@PathVariable Long id, @RequestBody UserDTO userDTO) {
        return Response.success(userService.updateUser(id, userDTO));
    }

    /**
     * 删除用户
     *
     * @param id 用户ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @SaCheckPermission("system:user:manage:delete")
    public Response<Void> deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return Response.success();
    }

    /**
     * 分页查询用户列表
     *
     * @param current 当前页
     * @param size 每页大小
     * @param username 用户名（模糊查询）
     * @param nickname 昵称（模糊查询）
     * @param phone 手机号（模糊查询）
     * @return 用户列表
     */
    @GetMapping
    @SaCheckPermission("system:user:manage:list")
    public Response<Page<UserListVO>> getUserList(
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long size,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String nickname,
            @RequestParam(required = false) String phone) {
        Page<User> page = new Page<>(current, size);
        return Response.success(userService.getUserList(page, username, nickname, phone));
    }

    /**
     * 根据ID查询用户
     *
     * @param id 用户ID
     * @return 用户信息
     */
    @GetMapping("/{id}")
    @SaCheckPermission("system:user:manage:query")
    public Response<User> getUserById(@PathVariable Long id) {
        return Response.success(userService.getUserById(id));
    }
}